/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  ZoneZhiDingQunZuActivity.java   
 * @Package com.jiyi.activity.zone   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-19 上午10:38:55   
 * @version V1.0     
 */
package com.jiyi.activity.zone;

import android.app.Dialog;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jiyi.adapter.DividerItemDecoration;
import com.jiyi.adapter.recyclerview.CommonAdapter;
import com.jiyi.adapter.recyclerview.base.ViewHolder;
import com.jiyi.bean.zone.ZoneQunZuBean;
import com.jiyi.bean.zone.ZoneQunZuListJson;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.inteface.WebApi;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;
import com.swift.utils.CustomDialogUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ZoneZhiDingQunZuActivity extends ActivityBase {

	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.refreshLayout)
	SwipeRefreshLayout refreshLayout;

	@BindView(R.id.mainList)
	RecyclerView mainList;

	List<ZoneQunZuBean> allQunZuDatas = new ArrayList<ZoneQunZuBean>();
	CommonAdapter mCommonAdapter;
	Dialog dialog;

	protected void onCreate(android.os.Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_zone_zhiding_qunzu);
		ButterKnife.bind(this);
		init();
	};
	
	@Override
	protected void onDestroy() {
		hideDialog(dialog);
		super.onDestroy();
	}

	private void init() {
		setTop();
		setAdapter();
		getQunZuData();
	}

	/**
	 * @author: HYM
	 * @Title: setAdapter
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-7-5
	 */
	private void setAdapter() {
		// 设置布局管理器
		mainList.setLayoutManager(new LinearLayoutManager(this));
		// 添加分割线
		mainList.addItemDecoration(new DividerItemDecoration(this,
				DividerItemDecoration.VERTICAL_LIST, null));
		// 初始化适配器
		mCommonAdapter = new CommonAdapter<ZoneQunZuBean>(this,
				R.layout.item_zone_qunzu, allQunZuDatas) {
			@Override
			protected void convert(ViewHolder holder, final ZoneQunZuBean t,
					int position) {
				holder.setImage(R.id.ivLogo, t.getGroup_head());
				holder.setText(R.id.tvQunZuName, t.getName());
				holder.setText(R.id.tvJoinNum, t.getMember() + "人");
				holder.setText(R.id.tvHuoDongNum, "发起活动：" + t.getActNum() + "次");
				holder.setVisible(R.id.tvDate, false);
				holder.setOnClickListener(R.id.rlRoot, new OnClickListener() {

					@Override
					public void onClick(View v) {
						final CustomDialogUtil ad = new CustomDialogUtil(
								ZoneZhiDingQunZuActivity.this);
						ad.setTitle("指定群组:" + t.getName());
						ad.setMessage("已选择 " + t.getName()
								+ " 作为指定参加活动群，确定后将只有该群组成员可以报名参与您的活动，是否确定？");
						ad.setPositiveButton("确定", new OnClickListener() {
							@Override
							public void onClick(View v) {
								ad.dismiss();
								Intent intent = new Intent();
								intent.putExtra("qunzu_obj",
										new Gson().toJson(t));
								setResult(RESULT_OK, intent);
								finish();
							}
						});
						ad.setNegativeButton("取消", new OnClickListener() {
							@Override
							public void onClick(View v) {
								ad.dismiss();
							}
						});
					}
				});
			}
		};
		// recycleview设置适配器
		mainList.setAdapter(mCommonAdapter);
	}

	/**
	 * @author: HYM
	 * @Title: setTop
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-6-12
	 */
	private void setTop() {
		tvBack.setText("");
		tvTitle.setText("指定群组");
		tvSearch.setVisibility(View.GONE);
	}

	@OnClick({ R.id.tvBack })
	public void onClick(View veiw) {
		Intent intent = null;
		switch (veiw.getId()) {
		case R.id.tvBack:
			finish();
			break;
		default:
			break;
		}
	}

	private void getQunZuData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		dialog = showDialog(dialog);
		VolleyRequest.StringRequestPost(this, WebApi.ZONE_QUNZU_LIST, "",
				params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ZoneQunZuListJson temp = new Gson().fromJson(
										result, ZoneQunZuListJson.class);
								if (temp == null || temp.getData() == null) {
									if (temp.getData().size() == 0) {
										refreshLayout.setVisibility(View.GONE);
									}
								} else {
									if (temp.getData().size() > 0) {
										allQunZuDatas.clear();
										allQunZuDatas.addAll(temp.getData());
									}

									mCommonAdapter.notifyDataSetChanged();
								}
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							refreshLayout.setRefreshing(false);
							hideDialog(dialog);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
						refreshLayout.setRefreshing(false);
						hideDialog(dialog);
					}
				});

	}

}
